/*
 * This class is the controller class for the reports. 
 */

package pos.control;

import java.math.BigDecimal;
import pos.Pos;
import pos.storage.db.mysql.SelectQueryTableModel;
import pos.storage.db.sql.SQL;

/**
 *
 * @author Parvez
 */
public class ReportsViewControl {
    
    /**
     * Generates the table for the views
     * 
     * @param sTableModel SelectQueryTableModel table to be displayed
     * @param sql String query to be executed
     */
    public void genTable(SelectQueryTableModel sTableModel, String sql){
        try{
            if(sql != null){
                sTableModel.genTable(Pos.adminConn, sql);
            }
            else{
                sTableModel.genTable(Pos.adminConn, SQL.SELECTITEM);
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }

    }
    
    /**
     * This method retrieves all the bills from the table and returns the sum
     * 
     * @param sTableModel SelectQueryTableModel table 
     * @return BigDecimal bill amount
     */
    public BigDecimal getBill(SelectQueryTableModel sTableModel){
        
        int ccount = sTableModel.getColumnCount();
        int colPos = 0;
        for(int i=0; i<ccount; i++){
            if("PurchasedItemBasePrice".equalsIgnoreCase(sTableModel.getColumnName(i))){
                colPos = i;
            }
        }
        int rcount = sTableModel.getRowCount();
        BigDecimal bd = new BigDecimal(0d);
        for(int i=0; i<rcount; i++){
            Double obj = (Double) sTableModel.getValueAt(i, colPos);
            bd = bd.add(new BigDecimal(obj));
        }

        return bd;
    }
}
